﻿using System;
using System.ComponentModel.DataAnnotations;

namespace Diploma.Models.Valildation.CustomAttributes
{
    [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property,
        AllowMultiple = false)]
    internal class PositiveNumberAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value == null)
            {
                return false;
            }

            int convertedValue;
            try
            {
                convertedValue = Convert.ToInt32(value);
            }
            catch (FormatException)
            {
                return false;
            }

            if (convertedValue >= 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}